import asyncio
import websockets
#import time
import json
#import yaml
import struct

def PackMsg(msgName, bytesData):
    pass
    msgType = 0
    nameBytes = msgName.encode('utf-8') + b'\x00'
    nameN = len(nameBytes)
    headLen = 12 + nameN
    totalLen = headLen + len(bytesData)

    #组装包头
    head = b''
    head = head + struct.pack('i', msgType)
    head = head + struct.pack('i', totalLen)
    head = head + struct.pack('i', nameN)

    #将[包头，协议名，数据]组装起来
    head = head + nameBytes + bytesData

    return head

def UnpackMsg(datas):
  
    head = struct.unpack("iii", datas[0:12])
    nameBytes = datas[12:12 + head[2] - 1]
    name = str(nameBytes, encoding = 'utf-8')

    #数据包数据
    pkgDatas = datas[12 + head[2]:]

    #分发数据函数 函数(self, name, pkgDatas)
    pkg = (name, pkgDatas)

    return pkg

def GetSC_SATELLITE_LIST_MSG():
    
    msgName = "SC_SATELLITE_LIST_MSG"
    data = {
        "name":msgName
    }
    data = json.dumps(data)
    data = data.encode('utf-8')
    data = struct.pack('I', len(data)) + data
    return PackMsg(msgName, data)

def GetSC_SATELLITE_MSG():
    
    msgName = "SC_SATELLITE_MSG"
    data = {
        "SAT_SN":"SAT00001"
    }
    data = json.dumps(data)
    data = data.encode('utf-8')
    data = struct.pack('I', len(data)) + data
    return PackMsg(msgName, data)

def GetSC_COLLECTION_MSG():
    
    msgName = "SC_COLLECTION_MSG"
    data = {
        "SAT_SN":"SAT00555"
    }
    data = json.dumps(data)
    data = data.encode('utf-8')
    data = struct.pack('I', len(data)) + data
    return PackMsg(msgName, data)

def GetSC_GNSS_MSG():
    
    msgName = "SC_GNSS_MSG"
    data = {
        "SAT_SN":"SAT00001"
    }
    data = json.dumps(data)
    data = data.encode('utf-8')
    data = struct.pack('I', len(data)) + data
    return PackMsg(msgName, data)

def GetSC_SEC_ISL_MSG():
    msgName = "SC_SEC_ISL_MSG"
    data = {
        "SEC_ISL_SAT1":"SAT00001",
        "SEC_ISL_SAT2":"SAT02713"
    }
    data = json.dumps(data)
    data = data.encode('utf-8')
    data = struct.pack('I', len(data)) + data
    return PackMsg(msgName, data)

def GetSC_SCENE_MSG(): 
    msgName = "SC_SCENE_MSG"
    data = {
        "name":msgName
    }
    data = json.dumps(data)
    data = data.encode('utf-8')
    data = struct.pack('I', len(data)) + data
    return PackMsg(msgName, data)

def GetSC_TIME_MSG():
    pass 
    msgName = "SC_TIME_MSG"
    data = {
        "SLICE_IDX":2
    }
    data = json.dumps(data)
    data = data.encode('utf-8')
    data = struct.pack('I', len(data)) + data
    return PackMsg(msgName, data)


async def ws_client_test(url):

    async with websockets.connect(url) as websocket:

        while True:

            #sentData = PackMsg("123", b'1234567')
            #sentData = GetSC_SATELLITE_LIST_MSG()
            #sentData = GetSC_SATELLITE_MSG()
            #sentData = GetSC_COLLECTION_MSG()
            #sentData = GetSC_GNSS_MSG()
            sentData = GetSC_SEC_ISL_MSG()
            #sentData = GetSC_SCENE_MSG()
            #sentData = GetSC_TIME_MSG()
            await websocket.send(sentData)
            response = await websocket.recv()
            #print(response)

            #解析协议头
            pos = int(12)
            head = struct.unpack("iii", response[0:12])
            #print(head)

            #解析名字
            nameBytes = response[12:12 + head[2] - 1]
            name = str(nameBytes, encoding = 'utf-8')
            #print(name)
            
            #跳过名字
            pos += head[2]

            print("total bytes:" + str(head[1]) + ",name byets: " + str(head[2]))

            name, pkgDatas = UnpackMsg(response)

            #test SC_SATELLITE_LIST_MSG
            # pkgDatas = pkgDatas[4:]
            # nnn = pkgDatas[110221 - 1]
            # data = json.loads(pkgDatas)

            #test SC_SATELLITE_MSG
            pkgDatas = pkgDatas[4:]
            print(pkgDatas)
            data = json.loads(pkgDatas)
            
            pass 



if __name__ == "__main__":

    #test
    #pkgData = GetSC_SATELLITE_LIST_MSG()
    #test

    #连接的地址
    url = "ws://localhost:33335/"

    #运行程序
    asyncio.run(ws_client_test(url))